//
//  AllBaseViewController.m
//  parent
//
//  Created by IreWesT on 15/5/28.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "AllBaseViewController.h"
#import "Masonry.h"
#import "IQKeyboardManager.h"

@interface AllBaseViewController ()

@end

@implementation AllBaseViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if (Current_device_vesion >= 7.0) {
            if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]){
                self.automaticallyAdjustsScrollViewInsets = NO;
            }
        }
    }
    return self;
}

- (void)loadView {
    
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = [UIColor whiteColor];
    self.view = view;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    _navHeight = 64.0f;
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = NO;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)backPage:(id)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - NavCenterButton

- (UIView *)createNavCenterSilderWithNames:(NSArray *)names withBlock:(void (^)(NSInteger index))block isWider:(BOOL)wider
{
    NSInteger size = names.count;
    _centerNavBlock = block;
    if (size < 2) {
        return nil;
    }
    
    float cellWidth = 61.0f;
    if (wider) {
        cellWidth = 91.0f;
    }
    float cellHeight = 28.0f;
    float width = cellWidth * size;
    float height = cellHeight + 2;
    if (size == 2) {
        width += 3;
    }else {
        width += 4;
    }
    
    int tag = 7000;
    UIView *backWrap = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    [backWrap setBackgroundColor:[UIColor whiteColor]];
    [backWrap.layer setCornerRadius:3.0f];
    [backWrap.layer setMasksToBounds:YES];
    [backWrap setTag:tag];
        
    UIButton *buttonFirst = [[UIButton alloc] initWithFrame:CGRectMake(1, 1, cellWidth, cellHeight)];
    [buttonFirst setBackgroundColor:[UIColor whiteColor]];
    [buttonFirst setTitle:names[0] forState:UIControlStateNormal];
    [buttonFirst setTitleColor:DefaultNavigationColor forState:UIControlStateNormal];
    [buttonFirst setShowsTouchWhenHighlighted:YES];
    [buttonFirst setTag:++tag];
    [buttonFirst.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
    [buttonFirst addTarget:self action:@selector(centerNavClick:) forControlEvents:UIControlEventTouchUpInside];
    [backWrap addSubview:buttonFirst];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:buttonFirst.bounds
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft
                                                         cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = buttonFirst.bounds;
    maskLayer.path = maskPath.CGPath;
    buttonFirst.layer.mask = maskLayer;
    
    if (size == 3) {
        UIButton *buttonMid = [[UIButton alloc] initWithFrame:CGRectMake(2 + cellWidth, 1, cellWidth, cellHeight)];
        [buttonMid setBackgroundColor:DefaultNavigationColor];
        [buttonMid setTitle:names[1] forState:UIControlStateNormal];
        [buttonMid setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [buttonMid setShowsTouchWhenHighlighted:YES];
        [buttonMid setTag:++tag];
        [buttonMid.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
        [buttonMid addTarget:self action:@selector(centerNavClick:) forControlEvents:UIControlEventTouchUpInside];
        [backWrap addSubview:buttonMid];
    }
    
    UIButton *buttonLast = [[UIButton alloc] initWithFrame:CGRectMake(1 + (cellWidth + 1) * (size - 1), 1, cellWidth, cellHeight)];
    [buttonLast setBackgroundColor:DefaultNavigationColor];
    [buttonLast setTitle:names[size - 1] forState:UIControlStateNormal];
    [buttonLast setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [buttonLast setShowsTouchWhenHighlighted:YES];
    [buttonLast setTag:++tag];
    [buttonLast.titleLabel setFont:[UIFont systemFontOfSize:15.0f]];
    [buttonLast addTarget:self action:@selector(centerNavClick:) forControlEvents:UIControlEventTouchUpInside];
    [backWrap addSubview:buttonLast];
    UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:buttonLast.bounds
                                                    byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight
                                                          cornerRadii:CGSizeMake(3, 3)];
    CAShapeLayer *maskLayer2 = [[CAShapeLayer alloc] init];
    maskLayer2.frame = buttonLast.bounds;
    maskLayer2.path = maskPath2.CGPath;
    buttonLast.layer.mask = maskLayer2;
    
    return backWrap;
}

- (void)setCenterNavSelectIndex:(NSInteger)index
{
    NSInteger tag = index;
    if (tag < 1) {
        return;
    }
    
    UIView *wrap = self.navigationItem.titleView;
    if (!wrap) {
        return;
    }
    for (int i = 1; i <= wrap.subviews.count; i++) {
        UIButton *button = (UIButton *)[wrap viewWithTag:7000+i];
        if (tag == i) {
            [button setTitleColor:DefaultNavigationColor forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor whiteColor]];
        }else {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setBackgroundColor:DefaultNavigationColor];
        }
    }
}

- (void)centerNavClick:(UIButton *)button
{
    NSInteger tag = button.tag - 7000;
    if (tag < 1) {
        return;
    }
    
    UIView *wrap = self.navigationItem.titleView;
    if (!wrap) {
        return;
    }
    for (int i = 1; i <= wrap.subviews.count; i++) {
        UIButton *button = (UIButton *)[wrap viewWithTag:7000+i];
        if (tag == i) {
            [button setTitleColor:DefaultNavigationColor forState:UIControlStateNormal];
            [button setBackgroundColor:[UIColor whiteColor]];
        }else {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [button setBackgroundColor:DefaultNavigationColor];
        }
    }
    
    if (_centerNavBlock) {
        _centerNavBlock(tag);
    }
}

#pragma mark - MBHud

- (void)showHud:(NSString *)title isDim:(BOOL)isDim {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    _hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    _hud.dimBackground = isDim;
    if(title.length > 0) {
        _hud.labelText = title;
    }
}

- (void)showHudComplete:(NSString *)title success:(BOOL)success {
    NSString *name = success ? @"success" : @"excla";
    _hud.dimBackground = NO;
    _hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
    _hud.mode = MBProgressHUDModeCustomView;
    if(title.length > 0) {
        _hud.labelText = title;
    }
    [_hud hide:YES afterDelay:1];
}

- (void)ShowExclaHud:(NSString *)title{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;

    _hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    [self showHudComplete:title success:NO];
}

- (void)ShowExclaHud2:(NSString *)title{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    _hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    [self showHudComplete:title success:YES];
}

- (void)hideHud {
    [_hud hide:YES];
}

- (UIButton *)createNavRightButton:(NSString *)showName target:(id)target action:(SEL)action
{
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    float fontsize=18.0f;
    if(showName.length>2){
        fontsize=16.0f;
    }
    button.titleLabel.font = [UIFont systemFontOfSize:fontsize];
    [button setTitle:showName forState:UIControlStateNormal];
    button.showsTouchWhenHighlighted = YES;
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    return button;
}

@end
